package com.dingup.homework.service.impl;

import com.dingup.homework.bean.DO.CorrectTeacher;
import com.dingup.homework.bean.DO.HwHomeworkDO;
import com.dingup.homework.bean.DTO.HwHomeworkDTO;
import com.dingup.homework.dao.HwHomeworkMapper;
import com.dingup.homework.service.HwHomeworkService;
import com.dingup.homework.service.TeacherCorrectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author jun 2018/11/8.
 */
@Service
public class HwHomeworkServiceImpl implements HwHomeworkService {

    @Autowired
    private HwHomeworkMapper hwHomeworkMapper;
    @Autowired
    private TeacherCorrectService teacherCorrectService;

    @Override
    public Boolean add(HwHomeworkDO hwHomeworkDO) {
        return hwHomeworkMapper.add(hwHomeworkDO);
    }

    @Override
    public Boolean update(HwHomeworkDO hwHomeworkDO) {
        return hwHomeworkMapper.update(hwHomeworkDO);
    }

    @Override
    public List<HwHomeworkDTO> listNormalHomework(String userId) {
        return hwHomeworkMapper.listNormalHomework(userId);
    }

    @Override
    public List<HwHomeworkDTO> listHistoryHomework(String userId) {
        return hwHomeworkMapper.listHistoryHomework(userId);
    }

    @Override
    public HwHomeworkDTO getById(Integer id) {
        return hwHomeworkMapper.getById(id);
    }

    @Override
    public Boolean delete(Integer id) {
        return hwHomeworkMapper.delete(id);
    }

    @Override
    public Boolean assignHomework(Integer id) {
        return hwHomeworkMapper.assignHomework(id);
    }

    @Override
    @Transactional
    public Boolean updateCorrectTeacher(Integer id, CorrectTeacher correctTeacher) {
        HwHomeworkDTO hwHomeworkDTO = new HwHomeworkDTO();
        hwHomeworkDTO.setId(id);
        hwHomeworkDTO.setCorrectTeacher(correctTeacher);
        //同时修改老师作业批改记录中的批改老师
        teacherCorrectService.updateStatusWhileUpdateHomeworkCorrectTeacher(id, correctTeacher);
        return hwHomeworkMapper.update(hwHomeworkDTO);
    }

    @Override
    public Integer getExpireCountFromYesterday(String userId) {
        Date correctDate = new Date();
        Integer count = hwHomeworkMapper.getExpireHomeworkCount(userId, correctDate);
        return count;
    }
}
